package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int createOrders(Orders orders) {
        int orderId = 0;
        try {
            CartDao cartDao = new CartDaoImpl();
            OrdersDao ordersDao = new OrdersDaoImpl();
            OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
            //开启事务
            DBUtil.beginTransaction();
            // 1. 根据用户的编号和商家的编号查询购物车列表
            // 创建Cart对象 从Orders对象中获取到userId和businessId并保存到Cart对象中;
            // List<Cart> list = 调用CartDao中的listCart(Cart c);
            Cart cart = new Cart();
            cart.setUserId(orders.getUserId());
            cart.setBusinessId(orders.getBusinessId());
            List<Cart> list =  cartDao.listCart(cart);
            //2. 创建订单并返回订单的编号（orderId）
            //int orderId = 调用OrdersDao中的saveOrder方法
            orderId = ordersDao.saveOrders(orders);
            //3. 向明细表中指添加数据
            List<OrderDetailet> orderDetailetList = new ArrayList<>();
            for (Cart c:list) {
                OrderDetailet orderDetailet = new OrderDetailet();
                orderDetailet.setOrderId(orderId);
                orderDetailet.setFoodId(c.getFoodId());
                orderDetailet.setQuantity(c.getQuantity());
                //将明细对象添加到集合中
                orderDetailetList.add(orderDetailet);
            }
            orderdetailetDao.saveBatch(orderDetailetList); //批量添加
            //4. 清除购物车
            cartDao.removeCart(cart);
            //5. 提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderDetailetDao = new OrderdetailetDaoImpl();
        try {
            //1. 根据订单ID查询到订单的信息(多对一：商家)
            orders = ordersDao.getOrdersById(orderId);
            //2. 根据订单的ID查询订单明细的信息
            List<OrderDetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
            orders.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderDetailetDao = new OrderdetailetDaoImpl();
        List<Orders> list = new ArrayList<>();
        try {
            //1. 根据用户的ID查询订单信息(多对一：商家)
            list = ordersDao.listOrdersByUserId(userId);
            //2. 查询多个订单的订单明细信息
            for(Orders o:list) {
                List<OrderDetailet> odList = orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
                o.setList(odList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return list;
    }
}
